<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author YDZ
-->
<template>
  <BasicDrawer
    v-bind="$attrs"
    :showFooter="true"
    :okAuth="'process:job:pmConnectwaterjob:edit'"
    @register="registerDrawer"
    @ok="handleSubmit"
    width="60%"
  >
    <template #title>
      <Icon :icon="getTitle.icon" class="pr-1 m-1" />
      <span> {{ getTitle.value }} </span>
    </template>
    <BasicForm @register="registerForm" />
    <template #footer>
      <BpmButton
        v-model:bpmEntity="record"
        bpmEntityKey="id"
        formKey="Flow_ConnectWaterJob"
        completeText="提交"
        :loading="loadingRef"
        :auth="'process:job:pmConnectwaterjob:edit'"
        @complete="handleSubmit"
        @success="handleSuccess"
        @close="closeDrawer"
      />
    </template>
  </BasicDrawer>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsProcessJobPmConnectwaterjobForm',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, ref, computed } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  import {
    PmConnectwaterjob,
    pmConnectwaterjobmoreSave,
    pmConnectwaterjobForm,
  } from '/@/api/process/job/pmConnectwaterjob';
  import { BpmButton } from '/@/components/Bpm';
  import { pmProjectinformationTreeData } from '/@/api/proj/info/pmProjectinformation';

  const emit = defineEmits(['success', 'register']);

  const { t } = useI18n('process.job.pmConnectwaterjob');
  const { showMessage } = useMessage();
  const record = ref<PmConnectwaterjob>({} as PmConnectwaterjob);
  const getTitle = computed(() => ({
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: t('完善信息'),
  }));

  const loadingRef = ref(false);

  const inputFormSchemas: FormSchema[] = [
    {
      label: t('申请信息'),
      // field: 'employeeInfo',
      component: 'FormGroup',
      colProps: { lg: 24, md: 24 },
    },
    {
      label: t('工程名称'),
      field: 'projectno',
      fieldLabel: 'projectname',
      component: 'TreeSelect',

      componentProps: {
        api: pmProjectinformationTreeData,
        params: { treeOper: '09' },
        canSelectParent: false,
        allowClear: false,
      },
      itemProps: { validateTrigger: 'blur' },
      dynamicDisabled: true,
    },
    {
      label: t('工程编码'),
      field: 'projectno',
      component: 'Input',
      componentProps: {
        maxlength: 50,
        disabled: true,
      },
      dynamicDisabled: true,
    },
    {
      label: t('承包商'),
      field: 'pmContractor.name',
      component: 'Input',
      componentProps: {
        maxlength: 50,
        disabled: true,
      },
      dynamicDisabled: true,
    },
    {
      label: t('停水时间'),
      field: 'connectwatertime',
      component: 'DatePicker',
      componentProps: {
        format: 'YYYY-MM-DD HH:mm',
        showTime: { format: 'HH:mm' },
      },
      dynamicDisabled: true,
    },
    {
      label: t('施工时间（小时）'),
      field: 'constructiontime',
      component: 'Input',
      componentProps: {},
      dynamicDisabled: true,
    },
    {
      label: t('接水类别'),
      field: 'name',
      component: 'Select',
      componentProps: {
        dictType: 'zf_conWater_type',
        allowClear: true,
      },
      dynamicDisabled: true,
      itemProps: { validateTrigger: 'blur' },
    },
    {
      label: t('接水管道口径'),
      field: 'pipesize',
      component: 'Select',
      componentProps: {
        dictType: 'zf_pipe_size',
        allowClear: true,
      },
      itemProps: { validateTrigger: 'blur' },
      dynamicDisabled: true,
      // rules: [{ pattern: /^\d+$/, message: t('请输入一个正整数') }],
    },
    {
      label: t('管道直径'),
      field: 'diameter',
      component: 'Input',
      componentProps: {
        maxlength: -1,
      },
      dynamicDisabled: true,
    },
    {
      label: t('行政区域'),
      field: 'administrativearea',
      component: 'Select',
      componentProps: {
        dictType: 'zf_Area',
        allowClear: true,
      },
      itemProps: { validateTrigger: 'blur' },
      dynamicDisabled: true,
    },
    {
      label: t('水源压力等级'),
      field: 'syyldj',
      component: 'Input',
      componentProps: {},
      required: true,
      dynamicDisabled: true,
    },
    {
      label: t('停水工作位置'),
      field: 'tssgwz',
      component: 'InputTextArea',
      componentProps: {
        rows: 5,
      },
      required: true,
      dynamicDisabled: true,
      colProps: { lg: 24, md: 24 },
    },
    {
      label: t('信息收集表'),
      field: 'dataMap',
      component: 'Upload',
      componentProps: {
        loadTime: computed(() => record.value.__t),
        bizKey: computed(() => record.value.id),
        bizType: 'ConnectWaterJobCreate_image',
        uploadType: 'file',
        isPreview: true,
      },
      colProps: { lg: 24, md: 24 },
      // 文件上传的必填验证实例
      // rules: [
      //   { required: true },
      //   {
      //     validator(_rule, value) {
      //       return new Promise((resolve, reject) => {
      //         const len = !value || value['pmConnectwaterjob_image__len'] || 0;
      //         if (len == 0) reject(t('请上传图片'));
      //         else resolve();
      //       });
      //     },
      //   },
      // ],
      dynamicDisabled: true,
    },

    {
      label: t('补充信息'),
      // field: 'employeeInfo',
      component: 'FormGroup',
      colProps: { lg: 24, md: 24 },
    },

    {
      label: t('阀门位置'),
      field: 'valveposition',
      component: 'Input',
      componentProps: {
        maxlength: -1,
      },
      required: true,
    },
    {
      label: t('停水工作位置'),
      field: 'cutoffarea',
      component: 'Input',
      componentProps: {
        maxlength: -1,
      },
      required: true,
    },
    {
      label: t('影响户数'),
      field: 'population',
      component: 'Input',
      componentProps: {
        maxlength: -1,
      },
      required: true,
      suffix: t('(户)'),
      colProps: { lg: 24, md: 24 },
    },
    {
      label: t('排水时长（小时）'),
      field: 'wdischargetime',
      component: 'InputNumber',
      componentProps: {
        maxlength: -1,
      },
      required: true,
    },
    {
      label: t('冲洗时长（小时）'),
      field: 'wsupplytime',
      component: 'InputNumber',
      componentProps: {
        maxlength: -1,
      },
      required: true,
    },
    {
      label: t('重点单位'),
      field: 'keyunit',
      colProps: { lg: 24, md: 24 },
      component: 'InputTextArea',
      componentProps: {
        maxlength: -1,
      },
      required: true,
    },
    {
      label: t('停水范围'),
      field: 'frontpressurearea',
      colProps: { lg: 24, md: 24 },
      component: 'InputTextArea',
      componentProps: {
        maxlength: -1,
      },
      required: true,
    },
    {
      label: t('备注说明'),
      field: 'remarks',
      component: 'InputTextArea',
      componentProps: {
        maxlength: -1,
      },
      colProps: { lg: 24, md: 24 },
    },
    {
      label: t('停水文件'),
      field: 'dataMap',
      component: 'Upload',
      componentProps: {
        loadTime: computed(() => record.value.__t),
        bizKey: computed(() => record.value.id),
        bizType: 'ConnectWaterJobMore_file',
        uploadType: 'all',
      },
      colProps: { lg: 24, md: 24 },
      // 文件上传的必填验证实例
      rules: [
        { required: true },
        {
          validator(_rule, value) {
            return new Promise((resolve, reject) => {
              const len = !value || value['ConnectWaterJobMore_file__len'] || 0;
              if (len == 0) reject(t('请上传附件'));
              else resolve();
            });
          },
        },
      ],
    },
    // {
    //   label: t('接水完成'),
    //   field: 'sponsordate',
    //   component: 'DatePicker',
    //   componentProps: {
    //     format: 'YYYY-MM-DD HH:mm',
    //     showTime: { format: 'HH:mm' },
    //   },
    // },
    // {
    //   label: t('接水结果'),
    //   field: 'connectwaterresult',
    //   component: 'Input',
    //   componentProps: {
    //     maxlength: -1,
    //   },
    // },
    // {
    //   label: t('OA表单ID'),
    //   field: 'wdischargeoano',
    //   component: 'Input',
    //   componentProps: {
    //     maxlength: -1,
    //   },
    // },
    // {
    //   label: t('wdischargeoapic'),
    //   field: 'wdischargeoapic',
    //   component: 'Input',
    //   componentProps: {
    //     maxlength: -1,
    //   },
    // },
    // {
    //   label: t('wastepipefile'),
    //   field: 'wastepipefile',
    //   component: 'Input',
    //   componentProps: {
    //     maxlength: -1,
    //   },
    // },
    // {
    //   label: t('wastepipe'),
    //   field: 'wastepipe',
    //   component: 'Input',
    //   componentProps: {
    //     maxlength: 8,
    //   },
    //   rules: [{ pattern: /^\d+$/, message: t('请输入一个正整数') }],
    // },
    // {
    //   label: t('gwwastepipefile'),
    //   field: 'gwwastepipefile',
    //   component: 'Input',
    //   componentProps: {
    //     maxlength: -1,
    //   },
    // },
    // {
    //   label: t('停水范围'),
    //   field: 'frontpressurearea',
    //   component: 'Input',
    //   componentProps: {
    //     maxlength: -1,
    //   },
    // },
    // {
    //   label: t('startimg'),
    //   field: 'startimg',
    //   component: 'Input',
    //   componentProps: {
    //     maxlength: -1,
    //   },
    // },
    // {
    //   label: t('worktime'),
    //   field: 'worktime',
    //   component: 'Input',
    //   componentProps: {
    //     maxlength: -1,
    //   },
    // },
    // {
    //   label: t('pipeinvolved'),
    //   field: 'pipeinvolved',
    //   component: 'Input',
    //   componentProps: {
    //     maxlength: -1,
    //   },
    // },
    // {
    //   label: t('接水现场图片'),
    //   field: 'scenepic',
    //   component: 'Input',
    //   componentProps: {
    //     maxlength: -1,
    //   },
    // },
    // {
    //   label: t('duration'),
    //   field: 'duration',
    //   component: 'Input',
    //   componentProps: {
    //     maxlength: -1,
    //   },
    // },
  ];

  const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
    labelWidth: 150,
    schemas: inputFormSchemas,
    baseColProps: { lg: 12, md: 24 },
  });

  const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
    setDrawerProps({ loading: true });
    await resetFields();
    const res = await pmConnectwaterjobForm({ ...data, ope: 'step2' });
    record.value = (res.pmConnectwaterjob || {}) as PmConnectwaterjob;
    if (record.value.pipesize != undefined || record.value.pipesize != null) {
      record.value.pipesize = record.value.pipesize + '';
    }
    record.value.__t = new Date().getTime();
    setFieldsValue(record.value);
    setDrawerProps({ loading: false });
  });

  async function handleSubmit() {
    try {
      const data = await validate();
      data.bpm = record.value.bpm; // 流程信息
      data.status = record.value.status; // 提交状态
      loadingRef.value = true;
      setDrawerProps({ confirmLoading: true });
      const params: any = {
        isNewRecord: record.value.isNewRecord,
        id: record.value.id,
        createDate: record.value.createDate,
      };
      // console.log('submit', params, data, record);
      const res = await pmConnectwaterjobmoreSave(params, data);
      showMessage(res.message);
      setTimeout(closeDrawer);
      emit('success', data);
    } catch (error: any) {
      if (error && error.errorFields) {
        showMessage(t('您填写的信息有误，请根据提示修正。'));
      }
      console.log('error', error);
    } finally {
      loadingRef.value = false;
      setDrawerProps({ confirmLoading: false });
    }
  }

  async function handleSuccess() {
    emit('success');
  }
</script>
